#include <iostream>
#include <string>
#include <cstdlib>
#include <unistd.h>

bool GetQueryString(std::string& query_string)
{
  bool result=false;
  std::string method=getenv("METHOD");
  if(method=="GET")
  {
    query_string=getenv("QUERY_STRING");
    result=true;
  }
  else if(method=="POST")
  {
    int content_length=atoi(getenv("CONTENT_LENGTH"));
    char ch=0;
    while(content_length)
    {
      read(0,&ch,1);
      query_string+=ch;
      content_length--;
    }

    result=true;
  }
  else
  {
    result=false;
  }

  return result;
}

void CutString(std::string& in,const std::string& sep,std::string& out1,std::string& out2)
{
  auto iter=in.find(sep);
  if(iter!=std::string::npos)
  {
    out1=in.substr(0,iter);
    out2=in.substr(iter+sep.size());
  }
}

int main()
{
  std::string query_string;
  GetQueryString(query_string);
  std::cerr<<query_string<<std::endl;

  std::string str1;
  std::string str2;
  CutString(query_string,"&",str1,str2);

  std::string name1;
  std::string value1;
  CutString(str1,"=",name1,value1);

  std::string name2;
  std::string value2;
  CutString(str2,"=",name2,value2);

  std::cout<<name1<<":"<<value1<<std::endl;
  std::cout<<name2<<":"<<value2<<std::endl;

  std::cerr<<name1<<":"<<value1<<std::endl;
  std::cerr<<name2<<":"<<value2<<std::endl;
  int x=atoi(value1.c_str());
  int y=atoi(value2.c_str());

  //可能想进行某种计算（计算、搜索、登陆），想进行某种存储（注册）
  std::cout<<"<html>";
  std::cout<<"<head><meta charset=\"utf-8\"></head>";
  std::cout << "<body>";
  std::cout << "<h3> " << value1 << " + " << value2 << " = "<< x+y << "</h3>";
  std::cout << "<h3> " << value1 << " - " << value2 << " = "<< x-y << "</h3>";
  std::cout << "<h3> " << value1 << " * " << value2 << " = "<< x*y << "</h3>";
  std::cout << "<h3> " << value1 << " / " << value2 << " = "<< x/y << "</h3>";
  std::cout << "</body>";
  std::cout << "</html>";

  //std::cerr<<"exit success"<<std::endl;

  return 0;
}
